package com.example.businessmanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.businessmanage.domain.dto.*;
import com.example.businessmanage.service.FosterService;
import com.example.common.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 代养控制器（代养人操作：发送意向、确认代养、申请返回等）
 */
@RestController
@RequestMapping("/api/v1/foster")
@Api(tags = "代养管理接口（代养人操作）")
@RequiredArgsConstructor
public class FosterController {

    private final FosterService fosterService;

    @PostMapping("/intent")
    @ApiOperation("发送代养意向（加入待选择代养人/宠物集合）")
    public Boolean sendFosterIntent(@Validated @RequestBody FosterIntentDTO fosterIntentDTO) {
        // 从UserContext获取当前登录代养人ID
        Long fosterUserId = UserContext.getUser();
        return fosterService.sendFosterIntent(fosterIntentDTO, fosterUserId);
    }

    @PostMapping("/confirm")
    @ApiOperation("确认/拒绝代养（确认改3-运输中，拒绝改2-已申请外借）")
    public Boolean confirmFoster(@Validated @RequestBody FosterConfirmDTO fosterConfirmDTO) {
        Long fosterUserId = UserContext.getUser();
        return fosterService.confirmFoster(fosterConfirmDTO, fosterUserId);
    }

    @PostMapping("/confirm-receive")
    @ApiOperation("确认接收宠物（改4-在代养人手里）")
    public Boolean confirmReceivePet(@Validated @RequestBody PetIdDTO petIdDTO) {
        Long fosterUserId = UserContext.getUser();
        return fosterService.confirmReceivePet(petIdDTO.getPetId(), fosterUserId);
    }

    @PostMapping("/apply-return")
    @ApiOperation("申请返回宠物（根据当前状态改6或7）")
    public Boolean applyReturnPet(@Validated @RequestBody PetIdDTO petIdDTO) {
        Long fosterUserId = UserContext.getUser();
        return fosterService.applyReturnPet(petIdDTO.getPetId(), fosterUserId);
    }

    @PostMapping("/agreement/upload")
    @ApiOperation("上传代养协议（阿里OSS URL）")
    public Boolean uploadAgreement(@Validated @RequestBody AgreementUploadDTO agreementUploadDTO) {
        Long fosterUserId = UserContext.getUser();
        return fosterService.uploadAgreement(agreementUploadDTO, fosterUserId);
    }

    @GetMapping("/my-foster")
    @ApiOperation("查询我的代养列表（分页，外借情况>1且负责人是自己）")
    public IPage<FosterBusinessRespDTO> getMyFoster(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long fosterUserId = UserContext.getUser();
        return fosterService.getMyFoster(fosterUserId, pageNum, pageSize);
    }

    @GetMapping("/to-be-selected-pet")
    @ApiOperation("查询我的待选择代养宠物列表（分页）")
    public IPage<PetSimpleRespDTO> getToBeSelectedPet(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long fosterUserId = UserContext.getUser();
        return fosterService.getToBeSelectedPet(fosterUserId, pageNum, pageSize);
    }
}
